package com.egdigital.allzic.service;

import android.media.AudioTrack;
import android.util.Log;

import com.spoledge.aacdecoder.MultiPlayer;
import com.spoledge.aacdecoder.PlayerCallback;

public class PlayerPub implements PlayerCallback {

	private static final String	TAG	= PlayerRadio.class.getSimpleName();	// log
	private MultiPlayer			mPlayer;
	private StreamingState		mState;
	private PubPlayerCallBack	mCallback;
	private AudioTrack			mAudioTrack;

	static interface PubPlayerCallBack {

		public void pubPlayerStarted();

		public void pubPlayerStopped(int perf);

		public void pubPlayerException(Throwable e);

		public void pubMetadata(String key, String value);

		public void pubPlayerPCMFeedBuffer(boolean isPlaying, int audioBufferSizeMs, int audioBufferCapacityMs);
	}

	public PlayerPub() {
		super();
	}

	/**
	 * callbacks PlayerRadio
	 */
	public void setCallbacks(PubPlayerCallBack callback) {
		mCallback = callback;
	}

	/**
	 * player commands
	 */
	public void start(String url) throws Exception {
		if (mPlayer == null) {
			mPlayer = new MultiPlayer(this);
		}
		mPlayer.playAsync(url);
		mState = StreamingState.PREPARING;
	}

	public void stop() throws Exception {
		if (mPlayer == null || isStopping() || isStopped() || isPreparing()) {
			throw new Exception();
		}
		mPlayer.stop();
		if (mAudioTrack != null) {
			mAudioTrack.release();
			mAudioTrack.flush();
			mAudioTrack = null;
		}
		setState(StreamingState.STOPPING);
		mPlayer = null;
	}

	public StreamingState getState() {
		return mState;
	}

	/**
	 * states
	 */
	public boolean isCreated() {
		return (mState == StreamingState.CREATED);
	}

	public boolean isEmpty() {
		return (mState == StreamingState.EMPTY);
	}

	public boolean isStopped() {
		return (mState == StreamingState.STOPPED);
	}

	public boolean isStarted() {
		return (mState == StreamingState.STARTED);
	}

	public boolean isPaused() {
		return (mState == StreamingState.PAUSED);
	}

	public boolean isPrepared() {
		return (mState == StreamingState.PREPARED);
	}

	public boolean isPreparing() {
		return (mState == StreamingState.PREPARING);
	}

	public boolean isStopping() {
		return mState == StreamingState.STOPPING;
	}

	public void setState(StreamingState state) {
		mState = state;
	}

	/**
	 * callbacks MultiPlayer
	 */
	@Override
	public void playerAudioTrackCreated(AudioTrack audioTrack) {
		mAudioTrack = audioTrack;
	}

	@Override
	public void playerException(Throwable e) {
		mState = StreamingState.ERROR;
		mCallback.pubPlayerException(e);
	}

	@Override
	public void playerMetadata(String key, String value) {
		mCallback.pubMetadata(key, value);
	}

	@Override
	public void playerPCMFeedBuffer(boolean isPlaying, int audioBufferSizeMs, int audioBufferCapacityMs) {
		mCallback.pubPlayerPCMFeedBuffer(isPlaying, audioBufferSizeMs, audioBufferCapacityMs);
	}

	@Override
	public void playerStarted() {
		if (!isStopping()) { // strange i know but this kind of behavior happens, so just in case
			mCallback.pubPlayerStarted();
		}
		mState = StreamingState.STARTED;
	}

	@Override
	public void playerStopped(int perf) {
		mCallback.pubPlayerStopped(perf);
		mState = StreamingState.STOPPED;
	}
}
